home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-18 | 2.8 KB | 87 lines | [TEXT/CWIE] |
- // ConstData.h
-
- #ifndef ConstData_h
- #define ConstData_h
-
- #ifndef Integers_h
- #include "Integers.h"
- #endif
- #ifndef Assert_h
- #include "Assert.h"
- #endif
-
- class Data;
-
- /*
- A ConstData is a range of bytes, starting at Start() and
- not including End(). You can't change them.
- */
-
- class ConstData
- {
- private:
- const uint8 *start;
- const uint8 *end;
-
- public:
- ConstData() : start(0), end(0) {}
-
- ConstData( const void *theStart, const void *theEnd )
- : start( static_cast<const uint8 *>( theStart ) ),
- end( static_cast<const uint8 *>( theEnd ) )
- {
- Assert( theEnd >= theStart );
- Assert( theStart != 0 || theEnd == 0 );
- }
-
- ConstData( const void *theStart, uint32 theLength )
- : start( static_cast<const uint8 *>( theStart ) ),
- end( static_cast<const uint8 *>( theStart ) + theLength )
- {
- Assert( theStart != 0 || theLength == 0 );
- }
-
- const uint8 *Start() const { return start; }
- const uint8 *End() const { return end; }
-
- uint32 Length() const { return end - start; }
-
- uint32 BoundedLength( uint32 bound ) const { return ( Length() <= bound ) ? Length() : bound; }
-
- bool IsEmpty() const { return start == end; }
- bool Null() const { return start == 0; }
-
- const uint8& operator[]( uint32 i ) const { Assert( i < Length() ); return start[i]; }
-
- ConstData Head( uint32 position ) const { Assert( position <= Length() ); return ConstData( start, start+position ); }
- ConstData Tail( uint32 position ) const { Assert( position <= Length() ); return ConstData( start+position, end ); }
- ConstData Middle( uint32 s, uint32 e ) const { Assert( s <= e ); Assert( e <= Length() ); return ConstData( start+s, start+e ); }
-
- void Truncate( uint32 position ) { Assert( position <= Length() ); end = start + position; }
- void LimitLength( uint32 limit ) { if ( limit < Length() ) end = start + limit; }
-
- void Shorten( uint32 amount ) { Assert( amount <= Length() ); start += amount; }
- void Lengthen( uint32 amount ) { end += amount; }
-
- bool StartsWith( ConstData ) const;
- bool EndsWith( ConstData ) const;
- bool Contains( ConstData ) const;
-
- bool Contains( const void *p ) const { return start <= p && p < end; }
-
- ConstData operator>>( Data ) const; // returns the remainder, for chaining
- };
-
- bool operator==( const ConstData& a, const ConstData& b );
- bool operator<( const ConstData& a, const ConstData& b );
- bool operator<=( const ConstData& a, const ConstData& b );
-
- inline bool operator!=( const ConstData& a, const ConstData& b ) { return !( a == b ); }
- inline bool operator>( const ConstData& a, const ConstData& b ) { return !( a <= b ); }
- inline bool operator>=( const ConstData& a, const ConstData& b ) { return !( a < b ); }
-
- int32 Compare( ConstData a, ConstData b );
- int32 CompareReversed( ConstData a, ConstData b );
-
- #endif
-